package cn.virens.library.widget.form.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import cn.virens.library.R;
import cn.virens.library.uitls.LogUtil;
import cn.virens.library.widget.form.Form;
import cn.virens.library.widget.form.FormException;
import cn.virens.library.widget.form.FormInputLayoutInter;
import cn.virens.library.widget.form.FormInputViewInter;

import static android.view.View.VISIBLE;
import static java.security.AccessController.getContext;


/**
 * 表单 输入控件组
 * Created by loioi on 2017/6/16.
 */
public class FormInputLayout extends FormTextLayout implements FormInputLayoutInter {
    private boolean ignore = false;

    public FormInputLayout(Context context) {
        this(context, null);
    }

    public FormInputLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FormInputLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this._initAttrs(attrs, defStyleAttr);
    }

    private void _initAttrs(AttributeSet attrs, int defStyleAttr) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.FormInputLayout, defStyleAttr, 0);

        this.ignore = a.getBoolean(R.styleable.FormInputLayout_ignore, false);

        a.recycle();
    }

    @Override
    public boolean isIgnore() {
        return ignore || (getVisibility() != VISIBLE);
    }

    public void setIgnore(boolean ignore) {
        this.ignore = ignore;
    }

    /**
     * 验证 - 所有输入框
     *
     * @return
     */
    @Override
    public void validate() throws FormException {
        if (isIgnore()) return;
        // 获取全部表单控件
        List<Form> list = getFormControls(this);
        // 遍历依次校验表单
        for (int i = 0; i < list.size(); i++) {
            Form view = list.get(i);

            if (view instanceof FormInputViewInter) {
                ((FormInputViewInter) view).validate();
            } else if (view instanceof FormInputLayoutInter) {
                ((FormInputLayoutInter) view).validate();
            } else {
                LogUtil.d("非表单控件");
            }
        }
    }

    /**
     * 获取 - 全部输入框值
     *
     * @return
     */
    @Override
    public Map<String, Object> getValue() {
        Map<String, Object> result = new HashMap<>();
        // 获取全部表单控件
        List<Form> list = getFormControls(this);
        // 遍历取出所有的值
        for (int i = 0; i < list.size(); i++) {
            Form view = list.get(i);

            if (view instanceof FormInputViewInter) {
                if (!((FormInputViewInter) view).isIgnore()) {
                    String name = ((FormInputViewInter) view).getName();
                    Object value = ((FormInputViewInter) view).getValue();

                    result.put(name, value);
                }
            } else if (view instanceof FormInputLayoutInter) {
                if (!((FormInputLayoutInter) view).isIgnore()) {
                    result.putAll(((FormInputLayoutInter) view).getValue());
                }
            }
        }
        return result;
    }
}
